<?php
/**
 * 常用地址管理
 * @author shitou
 *
 */
class SiteAction extends CommonAction {
	
	private $tableAddress  = "Site";        //定义地址表
	private $AddMemberView = "AddMemberView";        //定义地址表
	
 
    /**
     * 初始化
     */
    public function _initialize(){
    
    	parent::_initialize();
    	//parent::checkTimestamp();
    }
    /**
     * 查询地址（分页）
     */
    public function index(){
    	
    	$table = $this->tableAddress; 
    	$model = D($table);
    	
    	$cat['city_id']   = $this->_req("city_id","intval",0);
    	$cat['addid']     = $this->_req("addid","intval",0);
    	$cat['role_view'] = $this->_req("role_view","intval",0);
    	
    	if($cat['role_view']==1){
    		$map= array("addid"=>$cat['addid']);
    	}
    	
    	$desc     = $this->_req("orderDirection", "trim",   "desc");;
    	$order    = $this->_req("orderName",      "trim",   "addtime");
    	$pageNum  = $this->_req("pageNum",        "intval", C('PAGE_NUMS'));
    	$nowPage  = $this->_req("p",              "intval", 1);
    	$where    = $this->_req('where');
    	
    	if($where){
    		foreach ($where as $k=>$v){
    			if($v){
    				if($k=='keywords'){
    					$map['tags|keywords|address|enaddress'] = array("like",'%'.$v.'%');
    				}else{
    					$map[$k] = $v;
    				}
    			}
    		}
    	}
    	if($cat['city_id']){
    		$map['city'] = $cat['city_id'];
    	}
    	$map['isdel'] = 2;
    	$con      = $order.' '.$desc;
    	$this->success(parent::pageSelect($table,$map,$pageNum,$nowPage,$con));
    }
    

    /**
     * 添加地址
     */
    public function add(){
    	$param = array(
    		"before"  => "address_before_deal",
    		"after"   => "address_after_deal",
    	);
    	parent::do_add($this->tableAddress,$param);
    
    }
    /**
     * 添加用户地址
     */
    public function front_add(){
    	$param = array(
    		"before"  => "address_before_deal",
    	);
    	parent::do_add($this->tableAddress,$param);
    
    }
    /**
     * 修改地址
     */
    public function front_edit(){
    	$param = array(
    		"before"  => "address_before_deal",
    	);
    	parent::do_edit($this->tableAddress,$param);
    
    }
    /**
     * 修改地址
     */
    public function edit(){
    	$param = array(
    		"before" => "address_before_deal",
    		"after"  => "address_after_deal",
    	);
    	parent::do_edit($this->tableAddress,$param);
    
    }
    
    /**
     * 查询餐厅地址关联详情
     */
     public function detail(){
    
    	$id = $this->_get("id","intval",0);
    	$data  = M($this->tableAddress)->find($id);
    	$this->success($data);
    
    } 
    
    /**
     * 删除地址
     */
    public function del(){
    	
    	$id    = $this->_get("id","intval",0);
    	$model = M($this->tableAddress);
    	
    	//如果该地址已经在地图上标了点，那么要删除这个点
    	$view = $model->find($id);
    	if($view['mapid']){
    		$json  = mapStorge(array("id"=>$view['mapid']),"delete");
    		if($json['status'] === 0){
    			mapAll(true);
    		}
    	}
    
    	$maps = array("id"=>$id,"isdel"=>1);
    	$model->save($maps);
    }
    
    /**
     * 查询用户地址列表
     */
    public function byUser(){
    	 
    	$userid = $this->_get("userid","intval",0);
    	$map    = array("isdel"=>2,"userid"=>$userid);
    	$data = D($this->AddMemberView)->where($map)->select();
    	$this->success($data);
    }
    
    /**
     * 地址无分页查询
     */
    /*public function select(){
    	 
    	$keywords = $this->_req("keywords","trim","");
    	$cityid   = $this->_req("cityid","intval",0);
    	 
    	if($keywords){
    		$map['keywords'] = array("like","%".$keywords."%");
    	}
    	 
    	if($cityid){
    		$map['city'] = $cityid;
    	}
    	 
    	$map['isdel'] = 2;
    	 
    	$data = M($this->tableAddress)->where($map)->select();
    	$this->success($data);
    }*/
    
    /**
     * 地址无分页查询
     */
    public function select(){
    	
    	$keywords = $this->_req("keywords","trim","");
    	$cityid   = $this->_req("cityid","intval",0);
	
    	$where    = '';
    	
    	if($keywords){
    		
    		$arr   = explode(' ',$keywords);
    		$where = '(';
    		foreach($arr as $k=>$v){
    			$where .= "keywords like '%$v%' or address like '%$v%' or enaddress like '%$v%' or ";
    		}
    		$where  = substr($where, 0 ,-4);
    		$where .= ') and ';
    	}
    	
    	if($cityid){
    		$where .= 'city='.$cityid.' and ';
    	}
    	
    	$where .= 'isdel=2';
    	
    	$data = M($this->tableAddress)->where($where)->select();
    	$this->success($data);
    }
    
    /**
     * 添加备注信息点
     */
    public function outletLbs(){
    	 
    	$id      = $this->_req("id","intval",0);
    	$address = $this->_req("address","trim",0);
    	$array   = array();
    	 
    	if($id){
    		$model        = M("Site");
    		$array['row'] = $model->find($id);
    	}
    	$this->success($array);
    }
}